package com.foreknow.mapping;

import com.foreknow.model.Business;
import com.foreknow.model.Cart;
import com.foreknow.model.Food;
import java.sql.ResultSet;
import java.sql.SQLException;


public class CartMapping implements EntityMapping {
    @Override
    public Object mapping(ResultSet rs) throws SQLException {
        Cart cart = new Cart();
        cart.setCartId(rs.getInt("cartId"));
        cart.setFoodId(rs.getInt("foodId"));
        cart.setBusinessId(rs.getInt("businessId"));
        cart.setUserId(rs.getString("userId"));
        cart.setQuantity(rs.getInt("quantity"));
        //设置Cart与Food、Business的关联关系
        MappingFactory mappingFactory = MappingFactory.getInstance();
        EntityMapping foodMapping = mappingFactory.getMapping(MappingFactory.FOOD_MAPPING);
        EntityMapping businessMapping = mappingFactory.getMapping(MappingFactory.BUSINESS_MAPPING);
        Food food = (Food) foodMapping.mapping(rs);
        Business business = (Business) businessMapping.mapping(rs);
        cart.setFood(food);
        cart.setBusiness(business);
        return cart;
    }
}
